package studio.xiaoyun.core.parameter.criterion;

import java.util.LinkedList;
import java.util.List;

import studio.xiaoyun.core.parameter.Parameter;

public class CriterionAnd extends Criterion {
	private static final long serialVersionUID = -2246981220047251553L;
	private Criterion[] criterions;

	protected CriterionAnd(Criterion... criterions) {
		this.criterions = criterions;
	}

	@Override
	public List<String> getPropertyName() {
		List<String> list = new LinkedList<>();
		for(Criterion c:criterions){
			list.addAll(c.getPropertyName());
		}
		return list;
	}

	@Override
	public String getQuery(Parameter parameter) {
		StringBuffer result = new StringBuffer();
		for(Criterion c:criterions){
			if(c instanceof CriterionOr){
				result.append("(");
			}
			result.append(c.getQuery(parameter));
			if(c instanceof CriterionOr){
				result.append(")");
			}
			result.append(" and ");
		}
		result.delete(result.length()-5,result.length());
		return result.toString();
	}

}
